/*
 * @Description: utils
 * @Author: zoeblow
 * @Email: wangfuyuan@nnuo.com
 * @Date: 2019-12-16 13:47:28
 * @LastEditors: zoeblow
 * @LastEditTime: 2019-12-16 15:29:46
 * @FilePath: \monitor\utils\index.js
 */
const myAtob = require("atob");
module.exports = {
  b64EncodeUnicode: function(str) {
    return btoa(
      encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode("0x" + p1);
      })
    );
  },
  b64DecodeUnicode: function(str) {
    try {
      return decodeURIComponent(
        myAtob(str)
          .split("")
          .map(function(c) {
            return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
          })
          .join("")
      );
    } catch (e) {
      return str;
    }
  }
};
